package org.example.sdk;

@SuppressWarnings("unchecked")
public class NextTokenEncoderImpl implements NextTokenEncoder {

    @Override
    public String encode(Object nextToken) {
        if (nextToken == null) {
            return null;
        }
        MultiKey<Integer, Integer> k = (MultiKey<Integer, Integer>) nextToken;
        return k.getKey1() + "," + k.getKey2();
    }

    @Override
    public Object decode(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        Integer[] values = new Integer[2];
        String[] tmp = str.split(",");
        values[0] = Integer.parseInt(tmp[0]);
        values[1] = tmp.length > 1 ? Integer.parseInt(tmp[1]) : null;
        return new MultiKey<>(values[0], values[1]);
    }
}
